package com.jarvis.du.androidlibrarypro

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import com.jarvis.du.androidlibrarypro.bean.TestParcelable
import com.jarvis.du.androidlibrarypro.bean.WeatherVo
import com.jarvis.du.httpclient.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import java.util.*

class MainActivity : AppCompatActivity() {
    private var manager: RetrofitManager? = null;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("TAG", "---start---")
        val textpar = intent.extras.get("object") as TestParcelable
        Log.d("TAG", "---start---" + textpar.age)
        Log.d("TAG", "---start---" + textpar.name)
        Log.d("TAG", "---start---" + textpar.isDebug)
        manager = RetrofitManager()
        findViewById<Button>(R.id.btnHttp).setOnClickListener {
            val apiInterface = manager!!.createApi(RetrofitApiInterface::class.java)
            val maps: HashMap<String, String> = HashMap()
            maps.put("cityname", "苏州")
            maps.put("key", "c789752c67517e8f547fb454a6927a0b")
            maps.put("dtype", "json")
            val call: io.reactivex.Observable<WeatherVo> = apiInterface.createUser(maps)
            call.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DisposableObserver<WeatherVo>() {
                        override fun onNext(s: WeatherVo) {
                            Log.d("TAG", "---onNext---:" + s)
                            Thread.sleep(3000);
                        }

                        override fun onError(e: Throwable) {
                            Log.e("TAG", "---onError---:", e)
                        }

                        override fun onComplete() {
                            Log.d("TAG", "---onComplete---:")
                        }
                    })

        }
    }
}
